/*
* @Author: xfy
* @Date:   2017-08-29 22:28:57
* @Last Modified by:   xfy
* @Last Modified time: 2017-08-29 22:55:46
*/
'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var navSide         = require('page/common/nav-side/index.js');
var _supper         = require('util/supper.js');
var _user           = require('service/user-service.js');

//page逻辑部分
var page = {
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        //初始化左侧菜单
        navSide.init({
            name : 'user-pass-update'
        });
    },
    bindEvent : function(){
        var _this = this;
        //点击提交按钮后的动作
        $(document).on('click', '.btn-submit', function(){
            var userInfo = {
                password       : $.trim($('#password').val()),
                passwordNew       : $.trim($('#password-new').val()),
                passwordConfirm      : $.trim($('#password-confirm').val()),
                idNo                    : $.trim($('#idNo').val())
            },
            validateResult = _this.validateForm(userInfo);
            //验证成功
            if(validateResult.status){
                //更改用户密码
                _user.updatePassword({
                    oldPassword     : userInfo.password,
                    newPassword     : userInfo.passwordNew,
                    idNo            : userInfo.idNo
                }, function(res, msg){
                    _supper.successTips(msg);
                }, function(errMsg){
                    _supper.errorTips(errMsg);
                });
            }
            //验证失败
            else{
                _supper.errorTips(validateResult.msg);
            }
        });
    },
    //验证字段信息
    validateForm : function(formData){
        var result = {
            status  : false,
            msg     : ''
        };
        //验证原密码是否为空
        if(!_supper.validata(formData.password, 'require')){
            result.msg = '原密码不能为空';
            return result;
        }
        //验证新密码长度
        if(!formData.passwordNew || formData.passwordNew.length < 6){
            result.msg = '新密码为空或长度不足6位';
            return result;
        }
        //验证两次密码输入是否一致
        if(formData.passwordNew !== formData.passwordConfirm){
            result.msg = '两次输入的密码不一致';
            return result;
        }
        
        result.status   = true;
        result.msg      = '验证通过';
        return result;
    }
};
$(function(){
    page.init();
});